//
//  NewsViewController.m
//  棒棒哒
//
//  Created by rimi on 15/8/7.
//  Copyright (c) 2015年 rimi. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsTableViewCell.h"
#import "NetRequest.h"

@interface NewsViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIScrollView      *scrollView;
@property (nonatomic, strong) UITableView       *tableView;
@property (nonatomic, strong) NSArray           *titleArray;
@property (nonatomic, strong) NSMutableArray    *dataSource;

@end
static NSString *identifier = @"NEWS";
@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    [self netRequest];
    [self initializeDataSource];
    [self initializeUserInterface];
}

- (void)initializeDataSource
{
    _titleArray = @[@"社会热点", @"食品新闻", @"疾病快讯", @"药品新闻", @"生活贴士", @"医疗新闻", @"企业新闻"];
    _dataSource = [NSMutableArray array];
}

- (void)initializeUserInterface
{
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT / 8)];
    _scrollView.backgroundColor = [UIColor redColor];
    _scrollView.contentSize = CGSizeMake(120 * 7, 0);
    [self.view addSubview:_scrollView];
    
    for (int i = 0; i < 7; i ++) {
        UIButton *titleButton = [CustomButton buttonWithFrame:CGRectMake(0, 0, 0, 0) Title:_titleArray[i] BackGroudImage:nil BackGroundColor:[UIColor clearColor] TextColor:[UIColor whiteColor] CornerRadius:0 BorderWidth:0 BorderColor:nil];
        titleButton.bounds = CGRectMake(0, 0, 120, 40);
        titleButton.center = CGPointMake(i * 120 + 60, _scrollView.center.y);
        [_scrollView addSubview:titleButton];
    }
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, _scrollView.frame.size.height, SCREEN_WIDTH, SCREEN_HEIGHT) style:0];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView registerNib:[UINib nibWithNibName:@"NewsTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:identifier];
    [self.view addSubview:_tableView];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return _dataSource.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    return cell;
}

- (void)netRequest
{
//    NSString *idString = [NSString stringWithFormat:@"%ld", (long)_idNumber + 1];
//    NetRequest *newRequest = [[NetRequest alloc]init];
//    [newRequest request:NEWSDETAIL withHttpArg:[NSString stringWithFormat:@"id=%@", idString] success:^(id responseObject) {
//        NSLog(@"%@", responseObject);
//    } failure:^(NSError *error) {
//        
//    }];
    
    NSDictionary *dic = @{@"apikey":APIKEY, @"id":[NSString stringWithFormat:@"%ld", _idNumber]};
    [NetRequest GET:NEWSLIST parameters:dic success:^(id responseObject) {
        NSLog(@"%@", responseObject);
//        _dataSource = responseObject
    } failure:^(NSError *error) {
        
    }];
}

@end
